package cn.backpackerxl.service.impl;

import cn.backpackerxl.dao.StudentDao;
import cn.backpackerxl.dao.impl.StudentDaoImpl;
import cn.backpackerxl.entity.Student;
import cn.backpackerxl.pojo.PageStudent;
import cn.backpackerxl.service.StudentService;

import java.util.List;

/**
 * @author: backpackerxl
 * @create: 2021/12/8
 * @filename: StudentServiceImpl
 **/
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public PageStudent findAll(int currentPage, int pageSize) {
        PageStudent resPageStudent = studentDao.findAll(currentPage, pageSize);
        resPageStudent.setCurrentPage(currentPage);
        if (resPageStudent.getTotalStudent() % pageSize == 0) {
            resPageStudent.setTotalPage(resPageStudent.getTotalStudent() / pageSize);
        } else {
            resPageStudent.setTotalPage(resPageStudent.getTotalStudent() / pageSize + 1);
        }
        return resPageStudent;
    }

    @Override
    public int addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    @Override
    public PageStudent findByName(String name, int currentPage, int pageSize) {
        PageStudent resPageStudent = studentDao.findByName(name, currentPage, pageSize);
        resPageStudent.setCurrentPage(currentPage);
        if (resPageStudent.getTotalStudent() % pageSize == 0) {
            resPageStudent.setTotalPage(resPageStudent.getTotalStudent() / pageSize);
        } else {
            resPageStudent.setTotalPage(resPageStudent.getTotalStudent() / pageSize + 1);
        }
        return resPageStudent;
    }

    @Override
    public int updateStudent(Student student) {
        return studentDao.updateStudent(student);
    }

    @Override
    public int deleteStudent(int id) {
        return studentDao.deleteStudent(id);
    }

    @Override
    public Student findById(int id) {
        return studentDao.findById(id);
    }

    @Override
    public List<Student> downloadStudentInfo() {
        return studentDao.downloadStudentInfo();
    }
}
